/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.overall.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.overall.domain.SysGrade;
import me.zhengjie.modules.overall.repository.SysGradeRepository;
import me.zhengjie.modules.overall.service.SysGradeService;
import me.zhengjie.modules.overall.service.dto.SysGradeQueryCriteria;
import me.zhengjie.modules.reply.domain.ReplyCount;
import me.zhengjie.modules.reply.repository.ReplyCountRepository;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import me.zhengjie.utils.PageResult;
import me.zhengjie.modules.overall.service.dto.SysGradeDto;

/**
* @website https://eladmin.vip
* @author wzy
* @date 2024-11-28
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "al管理")
@RequestMapping("/api/sysGrade")
public class SysGradeController {

    private final SysGradeService sysGradeService;
    private final SysGradeRepository sysGradeRepository;
    @PostMapping("/saveScoreRule")
    @AnonymousAccess

    public ResponseEntity<Object> saveScoreRule(@RequestBody Map<String, Object>  scoreRuleDto) {
        Integer kaoqinWeight = Integer.parseInt(scoreRuleDto.get("kaoqinWeight").toString());
        Integer homeworkWeight = Integer.parseInt(scoreRuleDto.get("homeworkWeight").toString());
        Integer replycountWeight = Integer.parseInt(scoreRuleDto.get("replycountWeight").toString());
        Integer taskWeight = Integer.parseInt(scoreRuleDto.get("taskWeight").toString());
        System.out.println(scoreRuleDto);
        System.out.println(kaoqinWeight);
        List<SysGrade> replies = sysGradeRepository.findAll();

        for (SysGrade reply : replies) {
            Integer sum=0;
            sum=reply.getHomework()*homeworkWeight/100 +reply.getTask()*taskWeight/100 +
                    reply.getKaoqin()*kaoqinWeight/100 +reply.getReplycount()* replycountWeight/100;
            reply.setOverall(sum); // Ensure the ReplyCount entity has a `points` field
            sysGradeRepository.save(reply);
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('sysGrade:list')")
    public void exportSysGrade(HttpServletResponse response, SysGradeQueryCriteria criteria) throws IOException {
        sysGradeService.download(sysGradeService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询al")
    @ApiOperation("查询al")
    @PreAuthorize("@el.check('sysGrade:list')")
    public ResponseEntity<PageResult<SysGradeDto>> querySysGrade(SysGradeQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(sysGradeService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增al")
    @ApiOperation("新增al")
    @PreAuthorize("@el.check('sysGrade:add')")
    public ResponseEntity<Object> createSysGrade(@Validated @RequestBody SysGrade resources){
        sysGradeService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改al")
    @ApiOperation("修改al")
    @PreAuthorize("@el.check('sysGrade:edit')")
    public ResponseEntity<Object> updateSysGrade(@Validated @RequestBody SysGrade resources){
        sysGradeService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除al")
    @ApiOperation("删除al")
    @PreAuthorize("@el.check('sysGrade:del')")
    public ResponseEntity<Object> deleteSysGrade(@RequestBody Integer[] ids) {
        sysGradeService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
